package cn.edu.nuc.bre.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipInputStream;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import cn.edu.nuc.bre.service.interfaces.WorkFlowDefinationService;
import cn.edu.nuc.bre.util.PageInfo;

/**
 * 工作流部署定义相关的Action
 * @author 王凯
 *
 */
@Controller
@Scope("prototype")
public class WorkFlowDefinationAction extends ActionSupport{

	/**
	 * 序列化id
	 */
	private static final long serialVersionUID = -893860141864020040L;
	
	/**
	 * 工作流部署定义相关的业务对象
	 */
	@Autowired
	private WorkFlowDefinationService workFlowDefinationService;
	
	/**
	 * 目标页
	 */
	private Integer targetPage;
	
	/**
	 * 关键字
	 */
	private String keyword;
	
	/**
	 * 上传的文件(临时文件)
	 */
	private File upload;
	
	/**
	 * 上传文件的MIME类型
	 */
	private String uploadContentType;
	
	/**
	 *  文件名称
	 */
	private String uploadFileName;
	
	/**
	 * 部署id
	 */
	private String deploymentId;
	
	/**
	 * 图片名称
	 */
	private String imageName;
	
	public WorkFlowDefinationService getWorkFlowDefinationService() {
		return workFlowDefinationService;
	}

	public void setWorkFlowDefinationService(WorkFlowDefinationService workFlowDefinationService) {
		this.workFlowDefinationService = workFlowDefinationService;
	}

	public Integer getTargetPage() {
		return targetPage;
	}

	public void setTargetPage(Integer targetPage) {
		this.targetPage = targetPage;
	}
	
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	public String getDeploymentId() {
		return deploymentId;
	}
	
	public void setDeploymentId(String deploymentId) {
		this.deploymentId = deploymentId;
	}
	
	public String getImageName() {
		return imageName;
	}
	
	public void setImageName(String imageName) {
		this.imageName = imageName;
	}

	/**
	 * 部署新的流程
	 * @return
	 * @throws Exception 
	 */
	public String deployNewProcess(){
		
		try {
			//获得上传文件的输入流
			FileInputStream inputStream = new FileInputStream(upload);
			
			//转换为压缩流
			ZipInputStream zipInputStream = new ZipInputStream(inputStream);
			
			//部署流程
			workFlowDefinationService.deployNewProcess(zipInputStream, uploadFileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "list";
	}
	
	/**
	 * 获得部署信息
	 * @return
	 */
	public String listDeployment() {
		
		//如果没有设置目标页，默认访问第一页
		if (targetPage == null) {
			targetPage = 1;
		}
		
		//分页查询部署信息
		PageInfo<Deployment> pageInfo = workFlowDefinationService.findDeploymentByPage(targetPage, 5, keyword);
		
		//保存结果集
		ActionContext.getContext().put("pageInfo", pageInfo);
		
		//保存关键字用于回显
		ActionContext.getContext().put("keyword", keyword);
		return "success";
	}
	
	/**
	 * 删除部署信息
	 * @return
	 */
	public String deleteDeployment() {
		
		//删除部署的流程
		workFlowDefinationService.deleteProcessDefinitionByDeploymentId(deploymentId);
		
		return "list";
	}
	
	/**
	 * 获得流程定义信息（分页信息）
	 * @return
	 */
	public String listPorcessDefination() {
		
		//如果没有设置目标页，默认访问第一页
		if (targetPage == null) {
			targetPage = 1;
		}
		
		//分页查询流程定义
		PageInfo<ProcessDefinition> pageInfo = workFlowDefinationService.findPorcessDefinationByPage(targetPage, 5, keyword);
		
		//保存结果集
		ActionContext.getContext().put("pageInfo", pageInfo);
		
		//保存关键字用于回显
		ActionContext.getContext().put("keyword", keyword);
		return "success";
	}
	
	/**
	 * 查看流程图
	 * @throws Exception 
	 */
	public String viewImage() throws Exception{
		//1：获取页面传递的部署对象ID和资源图片名称(参数已经注入)
		
		//2：获取资源文件表（act_ge_bytearray）中资源图片输入流InputStream
		InputStream in = workFlowDefinationService.findImageInputStream(deploymentId,imageName);
		
		//3：从response对象获取输出流
		OutputStream out = ServletActionContext.getResponse().getOutputStream();
		
		//4：将输入流中的数据读取出来，写到输出流中
		for(int b=-1;(b=in.read())!=-1;){
			out.write(b);
		}
		out.close();
		in.close();
		
		//将图写到页面上，用输出流写
		return null;
	}
}
